package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.service.ChapterService;

/**
 * 课程文章restful接口及请求转发，支持跨域
 */
@RestController
@CrossOrigin
@RequestMapping("/api/chapter")
@Api(value="课程文章接口")
public class ChapterController {

	@Autowired
	private ChapterService chapterService;
	
	/**
	 * 课程目录接口
	 */
	@RequestMapping(value="/getCourseCatalog",method = RequestMethod.GET)
	@ApiOperation(value = "课程目录")
	public Map<String, Object> getCourseCatalog(
			@ApiParam(value="课程id",required=true) @RequestParam(value = "courseId",required=true)Integer courseId,
			@ApiParam(value="视频免费状态(0免费1收费)",required=false) @RequestParam(value = "freeStatus",required=false)Integer freeStatus){
		return chapterService.getCourseCatalog(courseId,"",freeStatus);
	}
	
	/**
	 * 根据课程ID取章节,同时把视频关联出来
	 */
	@RequestMapping(value="/getChapterByCourseId",method = RequestMethod.GET)
	@ApiOperation(value="根据课程ID取章节")
	@ApiIgnore
	public Map<String, Object> getChapterByCourseId(@ApiParam(value="课程id",required=true) @RequestParam(value = "courseId",required=true)Integer courseId,
			@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();

		return chapterService.getChapterByCourseId(co, userId, courseId);
	}	
}
